home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Applications / QuArK / plugins / mapfindtarget.py < prev    next >
Text File  |  2004-01-05  |  8KB  |  279 lines

  1. """   QuArK  -  Quake Army Knife
  2.  
  3. Python code to find things targetting and targetted by an entity
  4. """
  5. #
  6. # Copyright (C) 1996-99 Armin Rigo
  7. # THIS FILE IS PROTECTED BY THE GNU GENERAL PUBLIC LICENCE
  8. # FOUND IN FILE "COPYING.TXT"
  9. #
  10.  
  11. #$Header: /cvsroot/quark/runtime/plugins/mapfindtarget.py,v 1.4 2003/12/18 21:51:46 peter-b Exp $
  12.  
  13. Info = {
  14.    "plug-in":       "Target Finder",
  15.    "desc":          "Finds entities targetting and targetted by something",
  16.    "date":          "May 11 2002",
  17.    "author":        "tiglari",
  18.    "author e-mail": "tiglari@hexenworld.com",
  19.    "quark":         "Version 6.3" }
  20.  
  21.  
  22. from quarkpy.maputils import *
  23. import quarkpy.mapmenus
  24. import quarkpy.mapcommands
  25. import quarkpy.mapentities
  26. import quarkpy.dlgclasses
  27. from quarkpy import guiutils
  28. import quarkpy.mapsearch
  29.  
  30. import mapmadsel
  31. import quarkx
  32.  
  33. #
  34. # A `Live Edit' dialog.  Note the action buttons, which
  35. #   use a rather convoluted technique to produce their
  36. #   effects (when a button is bushed,
  37. #   quarkpy.qmacro.MACRO_zapview is executed with an index
  38. #   saying which, this calls the appropriate function of the
  39. #   dialog, which was attached to the editor when executed).
  40. #
  41. # A ListBox would be better than a ComboBox for data entry,
  42. #   but FormCFG.pas doesn't at the moment support ListBoxes.
  43. #   In principle, this could be fixed.
  44. #
  45.  
  46. class FindTargetDlg (quarkpy.dlgclasses.LiveEditDlg):
  47.     #
  48.     # dialog layout
  49.     #
  50.  
  51.     endcolor = AQUA
  52.     size = (240,190)
  53.     dfsep = 0.40
  54.  
  55.     dlgdef = """
  56.         {
  57.         Style = "9"
  58.         Caption = "Target Finder"
  59.  
  60.        found: = {
  61.           Typ = "C"
  62.           Txt = "List of found:"
  63.           Items = "%s"
  64.           Values = "%s"
  65.           Hint = "These are the entities found.  Pick one," $0D " then push buttons on row below for action."
  66.         }
  67.  
  68.         num: = {
  69.           Typ = "EF1"
  70.           Txt = "# found"
  71.         }
  72.           
  73.         sep: = { Typ="S" Txt=""}
  74.  
  75.         buttons: = {
  76.         Typ = "PM"
  77.         Num = "1"
  78.         Macro = "viewtargetted"
  79.         Caps = "I"
  80.         Txt = "Actions:"
  81.         Hint1 = "Inspect the chosen one"
  82.         }
  83.         
  84.         sep: = { Typ="S" Txt=""}
  85.  
  86.         specific: = {
  87.           Typ = "E"
  88.           Txt = "specific:"
  89.           Hint = "This is the targetting specific whose value is being checked"
  90.         }
  91.         
  92.         value: = {
  93.           Typ = "E"
  94.           Txt = "value:"
  95.           Hint = "Things with value of the specific above are to be found"
  96.         }
  97.         
  98.         sep: = { Typ="S" Txt=""}
  99.  
  100.         exit:py = {Txt="" }
  101.     }
  102.     """
  103.  
  104.     def inspect(self):
  105.         index = eval(self.src["found"])
  106.         #
  107.         # FIXME: dumb hack, revise mapmadsel
  108.         #
  109.         m = qmenu.item("",None)
  110.         m.object=self.pack.found[index]
  111.         mapmadsel.ZoomToMe(m)
  112.         mapmadsel.SelectMe(m)
  113.         #
  114.         # Some grotty crap to set the mpp to the face page
  115.         #
  116.         Spec1 = qmenu.item("", quarkpy.mapmenus.set_mpp_page, "")
  117.         Spec1.page = 1 # specifics page
  118.         quarkpy.mapmenus.set_mpp_page(Spec1) 
  119.         
  120.  
  121. #
  122. # Define the viewtargetted macro here, put the definition into
  123. #  quarkpy.qmacro, which is where macros called from delphi
  124. #  live.
  125. #
  126. def macro_viewtargetted(self, index=0):
  127.     editor = mapeditor()
  128.     if editor is None: return
  129.     if index==1:
  130.         editor.findtargetdlg.inspect()
  131.         
  132. quarkpy.qmacro.MACRO_viewtargetted = macro_viewtargetted
  133.  
  134.            
  135. def getSpecVal(spec, val, editor):
  136.     found = []
  137.     for item in editor.Root.findallsubitems("",":b")+editor.Root.findallsubitems("",":e"):
  138.         if item[spec]==val:
  139.             found.append(item)
  140.     return found
  141.     
  142.  
  143. def findClick(m, spec=None, val=None):
  144.     editor=mapeditor()
  145.     #
  146.     # Here we start the Live Edit dialog invocation sequence.
  147.     #  Data to be tracked during the life of the dialog goes
  148.     #  here.  
  149.     #
  150.     class pack:
  151.         "stick stuff in this"
  152.     pack.slist = []
  153.     pack.klist = []
  154.     pack.seen = 0
  155.         
  156.     def setup(self, pack=pack, editor=editor, spec=spec, val=val):
  157.         self.pack=pack
  158.         #
  159.         # Part of the convolution for the buttons, to communicate
  160.         #  which objects methods should be called when one pushed.
  161.         # Cleaned up in onclosing below.
  162.         #
  163.         editor.findtargetdlg=self
  164.         
  165.         if self.src['specific'] is None:
  166.             if spec:
  167.                 self.src['specific']=spec
  168.             
  169.         if self.src['value'] is None:
  170.             if val:
  171.                 self.src['value']=val
  172.             
  173.         if self.src['value']:
  174.             targetted = getSpecVal(spec,self.src['value'], editor)
  175.             ran = range(len(targetted))
  176.             pack.slist = map(lambda obj,num:"%d) %s:%s"%(num+1, obj.parent.shortname,obj.shortname), targetted, ran)
  177.             pack.klist = map(lambda d:`d`, ran)
  178.  
  179.             #
  180.             #  wtf doesn't this work, item loads but function is trashed
  181.             #
  182.              
  183.             self.src["found$Items"] = "\015".join(pack.slist)
  184.             self.src["found$Values"] = "\015".join(pack.klist)
  185.             self.src["num"]=len(pack.klist),
  186.             if not pack.seen and len(ran)>0:
  187.                 pack.seen = 1
  188.                 self.src["found"] = '0'
  189.                 self.chosen = '0'
  190.             elif len(ran)==0:
  191.                 self.src["found"] = ''
  192.                 self.chosen = ''
  193.             pack.found = targetted
  194.  
  195.     #
  196.     # When data is entered, this gets executed.
  197.     #
  198.     def action(self, pack=pack, editor=editor):
  199.         pass
  200.  
  201.     #
  202.     # Cleanup when dialog closes (not needed if no mess has
  203.     #  been created)
  204.     #
  205.     def onclosing(self,editor=editor):
  206.         del editor.findtargetdlg
  207.         
  208.     #
  209.     # And here's the invocation. 2nd arg is a label for storing
  210.     #  position info in setup.qrk.
  211.     #
  212.     FindTargetDlg(quarkx.clickform, 'findtarget', editor, setup, action, onclosing)
  213.  
  214.  
  215. def targetpopup(o):
  216.  
  217.     #
  218.     # If the entity we're clicking on has a target specific, we might
  219.     #  want things with the same targetname value, and vice-versa,
  220.     #  this target/targetname interchange is inherently confusing.
  221.     #
  222.     def targettingClick(m, o=o):
  223.         findClick(m, 'target', o['targetname'])
  224.  
  225.     def targettedClick(m, o=o):
  226.         findClick(m, 'targetname', o[m.spec])
  227.         
  228.     targettingItem = qmenu.item('entities targetting this one', targettingClick, "|Find entities targetting this entity")
  229.     if o['targetname'] is None:
  230.         targettingItem.state=qmenu.disabled
  231.  
  232.     list = [targettingItem]
  233.     
  234.     for specific in o.dictspec.keys():
  235.         if specific[-6:] == 'target':
  236.             item = qmenu.item('entities targetted by '+specific, targettedClick, "|Find entities targetted by the specific %s"%specific)
  237.             item.spec = specific
  238.             list.append(item)
  239.  
  240.     return qmenu.popup("Find targetted/ing", list, None,
  241.        "|commands for finding entities targetting and targetted by this one")
  242.  
  243.  
  244. def entmenu(o, editor, oldmenu=quarkpy.mapentities.EntityType.menu.im_func):
  245.     "point entity menu"
  246.     menu = oldmenu(o, editor)
  247.  
  248.     menu[:0] = [targetpopup(o),
  249.                 qmenu.sep]
  250.     return menu
  251.  
  252. quarkpy.mapentities.EntityType.menu = entmenu
  253.  
  254. def brushmenu(o, editor, oldmenu=quarkpy.mapentities.BrushEntityType.menu.im_func):
  255.     "brush entity menu"
  256.     menu = oldmenu(o, editor)
  257.  
  258.     menu[:0] = [targetpopup(o),
  259.                 qmenu.sep]
  260.     return menu
  261.  
  262. quarkpy.mapentities.BrushEntityType.menu = brushmenu
  263.  
  264.  
  265. #$Log: mapfindtarget.py,v $
  266. #Revision 1.4  2003/12/18 21:51:46  peter-b
  267. #Removed reliance on external string library from Python scripts (second try ;-)
  268. #
  269. #Revision 1.3  2002/05/21 07:06:10  tiglari
  270. #fix problems with selection dialog (loadiing first selection when appropriate)
  271. #
  272. #Revision 1.2  2002/05/20 11:07:54  tiglari
  273. #fix bug whereby if all targetted items had the same name, the first one
  274. #  of that name would be selected.  Also now preloading with first element works
  275. #
  276. #Revision 1.1  2002/05/18 05:21:39  tiglari
  277. #Suggestion by quantum_red
  278. #
  279.